package com.zuo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuo.entity.Pos;
import com.zuo.entity.User;
import com.zuo.service.PositionService;
import com.zuo.util.HrmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/2 18:25
 */
@Controller
public class PositionController {

    @Autowired
    private PositionService positionService;

    @RequestMapping("/job")
    public String position(Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page, HrmConstants.PAGE_DEFAULT_SIZE);

        List<Pos> pos = positionService.listPosition();

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Pos> pageInfo = new PageInfo<>(pos);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("positions", pos);
        model.addAttribute("type","all");
        return "job/job.jsp";
    }

    @RequestMapping("/searchJob")
    public String searchJob(String pname,Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<Pos> pos = positionService.searchPosition(pname);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Pos> pageInfo = new PageInfo<>(pos);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("positions", pos);
        model.addAttribute("type","search");
        model.addAttribute("pname",pname);
        return "job/job.jsp";
    }

    @RequestMapping("/toUpdateJob")
    public String toUpdateJob(int id,Model model){
        Pos pos = positionService.getOne(id);
//        System.out.println(position);
        model.addAttribute("pos", pos);
        return "job/showUpdateJob.jsp";
    }

    @RequestMapping("/updateJob")
    public String updateJob(Pos pos, Model model){
        int result = positionService.updatePosition(pos);
        if(result!=0){
            model.addAttribute("info","成功修改了"+result+"条数据");
        }else{
            model.addAttribute("info","修改失败");
        }
        return "forward:job.action";
    }

    @RequestMapping("/removeJob")
    public String removeJob(String ids,Model model){
        int result = positionService.removePositions(ids);
        model.addAttribute("info","成功删除了"+result+"条记录");
        return "forward:job.action";
    }

    @RequestMapping("/toAddJob")
    public String toAddJob(){
        return "job/showAddJob.jsp";
    }

    @RequestMapping("/addJob")
    public String addJob(Pos pos, Model model){
        int result = positionService.addPosition(pos);
        if(result!=0){
            model.addAttribute("info","添加成功");
        }else{
            model.addAttribute("info","添加失败");
        }
        return "forward:job.action";
    }


}
